﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BookstoreDesktopClient
{
    public partial class AddUser : Form
    {
        AccountWS.AccountWS account;
        BookstoreClient bookstoreClient;
        string sessionID;

        public AddUser(BookstoreClient bookstoreClient, AccountWS.AccountWS account, string sessionID)
        {
            InitializeComponent();
            this.bookstoreClient = bookstoreClient;
            this.account = account;
            this.sessionID = sessionID;
        }

        private void addUserButton_Click(object sender, EventArgs e)
        {
            
            if ((passwordMaskedTextBox.Text == confirmMaskedTextBox.Text))
            {
                try
                {
                    account.adduserAsync(
                        usernameTextBox.Text,
                        passwordMaskedTextBox.Text,
                        roleComboBox.SelectedItem.ToString(),
                        sessionID);

                    account.adduserCompleted += new AccountWS.adduserCompletedEventHandler(account_adduserCompleted);
                    
                }
                catch (Exception exc)
                {
                    MessageBox.Show("The form has been not fulfilled correctly!","Error",MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    exc.ToString();
                }
                this.Dispose(false);
            }
            else
            {
                MessageBox.Show(
                    "The given password does not match", 
                    "Warning", 
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Warning
                );
            }
            
        }

        void account_adduserCompleted(object sender, AccountWS.adduserCompletedEventArgs e)
        {
            bookstoreClient.refreshListOfUsers();

            if (e.@return) bookstoreClient.infoUserLabel.Text = "New user has been added.";
            else bookstoreClient.infoUserLabel.Text = "ERROR: User has NOT been added: Given username has existed in database!";
        }
    }
}
